package com.lingtao.shiqi;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TestJDBCContext {
    String url ="jdbc:mysql://localhost:3306/sys?characterEncoding=utf8";
    String driver="com.mysql.jdbc.Driver";
    String user = "root";
    String password = "root";
    Connection conn=null;
    PreparedStatement ps=null;
    ResultSet resultSet=null;

    public void connectionDB(){

        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, user, password);
            System.out.println("Successfully connected");
        } catch (ClassNotFoundException e) {

            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void  WriteContextIntoDB(){
        //1.将大文本插入到数据库，只需要读出 文本插入即可
        String insertsql="insert into tb_context(context) value(?)";
        try {
            ps = conn.prepareStatement(insertsql);
            File file = new File("D:\\王桑.txt");
            FileReader fileReader = new FileReader(file);
            //使用setCharacterStream()设置字符流
            ps.setCharacterStream(1, fileReader,(int) file.length());
            ps.executeUpdate();
            System.out.println("成功向数据库插入文本 ");
        } catch (SQLException e) {
            System.out.println("SQLException");
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            System.out.println("FileNotFoundException ");
            e.printStackTrace();
        }

    }

    //2.从数据读出文本内在写到磁盘
    public void  ReadContextFromDB(){

        try {
            String sql = "select*from tb_context where id=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, 1);
            ps.executeQuery();
            resultSet = ps.getResultSet();

            if (resultSet.next()) {
                Reader reader = resultSet.getCharacterStream("context");
                //写入到目标文件
                FileWriter fileWriter = new FileWriter(new File(
                        "D:\\inputContext.txt"));
                char[] buffer = new char[1024];
                int len = 0;
                while ((len = reader.read(buffer)) != -1) {
                    fileWriter.write(buffer, 0, len);
                }
                reader.close();
                fileWriter.close();
            }

        } catch (SQLException e) {
            System.out.println("SQLException");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("IOException");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws SQLException {

        TestJDBCContext t = new TestJDBCContext();
        t.connectionDB();
        t.WriteContextIntoDB();
        t.ReadContextFromDB();
        System.out.println("读取成功，请到D盘查看");

    }
}
